const {
  MemberModel
} = require('../../utils/member-model.js')
const memberModel = new MemberModel()
const app = getApp()
Component({
  properties: {
    activeIndex: {
      type: Number,
      value: 0
    },
    menus: {
      type: Array,
      value: []
    },
    hasSvip: {
      type: Boolean,
      value: false
    }
  },
  data: {
    menuHasSvip: false,
    shopclosetips: false
  },
  observers: {
    hasSvip(val) {
      this.setData({
        menuHasSvip: val
      })
    }
  },
  attached() {
    this.setData({
      menuHasSvip: this.data.hasSvip || wx.getStorageSync('menuHasSvip')
    })
  },
  methods: {
    changeTab(e) {
      this.goPage(e.currentTarget.dataset.index)
    },
    goPage(index) {
      let that = this;
      if (that.data.activeIndex !== index) {
        const urls = {
          '1': '/pages/index/index',
          '2': '/pages/svip/purchase/index',
          '3': '/pages/memberCenter/index/index'
        }
        let pageType = that.data.menus[index].options.nav_redirect_type
        let shopInfo = wx.getStorageSync('shopInfo')
        if (shopInfo && shopInfo.statusNew === '已撤店' && parseInt(pageType) === 2) {
          this.setData({
            shopclosetips:true
          })
          setTimeout(() => {
            this.setData({
              shopclosetips:false
            })
          },2000)
          return
        }
        this.goMenu(parseInt(pageType))
        if (parseInt(pageType) === 3 && app.globalData.isLogin) {
          this.getSubscribeMessage()
          wx.reLaunch({
            url: urls[pageType],
          })
        } else {
          wx.reLaunch({
            url: urls[pageType],
          })
        }
      }
    },
    goMenu(index) {
      if (index == 1) {
        wx.reportAnalytics('page_home_enter')
      } else if (index == 2) {
        wx.reportAnalytics('page_svip_enter')
      } else if (index == 3) {
        wx.reportAnalytics('page_member_enter')
      }
    },
    
    /**
     * 1.点击【我知道了】显示订阅消息弹窗
     * 2.点击【积分商城】进入积分详情页面时
     * 3.会员授权成功后
     * 4.点击【会员中心】按钮
     */
    getSubscribeMessage: function () {
      memberModel.subscribeMessage({
        merchantId: wx.getStorageSync('merchantId'),
        position: 4
      }).then(data => {
        if (data && data.length > 0) {
          let tmplId = []
          data.forEach(element => {
            if (element.templateId) {
              tmplId.push(element.templateId)
            }
          })
          if (tmplId.length > 0) {
            wx.requestSubscribeMessage({
              tmplIds: tmplId,
              success(res) {},
              fail(err) {}
            })
          }
        }
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    },
    formSubmit(e) {
      if (this.data.activeIndex !== 1 && app.globalData.isLogin) {
        memberModel.userMessage({
          formIds: [e.detail.formId],
          merchantId: wx.getStorageSync('merchantId')
        })
      }
    }
  }
})